{# Stalker Pyramid a Web Base Production Asset Management System
 Copyright (C) 2009-2014 Erkan Ozgur Yilmaz
 
 This file is part of Stalker Pyramid.
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation;
 version 2.1 of the License.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#}
<div class='dijitDialogPaneContentArea'>
    <form id='filename_template_form'>
        <table style='width: 100%;'>

            {# NAME #}
            <tr>
                <td class='label_column'>
                    <label for='filename_template_name'>
                        Name
                    </label>
                </td>
                <td class='input_column'>
                    <input id='filename_template_name'>
                </td>
            </tr>

            {# TARGET ENTITY TYPE #}
            <tr>
                <td class='label_column'>
                    <label for='filename_template_target_entity_type'>
                        Target Entity Type
                    </label>
                </td>
                <td class='input_column'>
                    <input id='filename_template_target_entity_type'>
                </td>
            </tr>

            {# PATH #}
            <tr>
                <td class='label_column'>
                    <label for='filename_template_path'>
                        Path Template
                    </label>
                </td>
                <td class='input_column'>
                    <input id='filename_template_path'>
                </td>
            </tr>

            {# FILENAME #}
            <tr>
                <td class='label_column'>
                    <label for='filename_template_filename'>
                        Filename Template
                    </label>
                </td>
                <td class='input_column'>
                    <input id='filename_template_filename'>
                </td>
            </tr>

        </table>
    </form>
</div>
<div class='dijitDialogPaneActionBar'>
    <button id='filename_template_ok_button'></button>
    <button id='filename_template_cancel_button'></button>
</div>
<script type='text/javascript'>
    require([
        'dojo/store/Memory',
        'dijit/form/Form',
        'dijit/form/ValidationTextBox',
        'dijit/form/TextBox',
        'dijit/form/FilteringSelect',
        'dijit/form/Button',

        'stalker/js/submitForm',

        'dojo/domReady!'
    ], function (Memory, Form, ValidationTextBox, TextBox, FilteringSelect,
                 Button, submitForm) {

        // ********************************************************************
        // Generate default values
        var url;
        var name;
        var target_entity_type;
        var path;
        var filename;
        var additional_data = {};

        {% if mode=='CREATE' %}
            url = '{{ request.route_url('create_filename_template') }}';
        {% elif mode=='UPDATE' %}
            url = '{{ request.route_url('update_filename_template', id=ft.id) }}';
            name = '{{ ft.name }}';
            target_entity_type = '{{ ft.target_entity_type }}';
            path = '{{ ft.path }}';
            filename = '{{ ft.filename }}';
            additional_data = {
                ft_id: {{ ft.id }}
            };
        {% endif %}

        // **********************************************************************
        // Form
        var filename_template_form = new Form({}, 'filename_template_form');
        filename_template_form.startup();

        var dialog = filename_template_form.getParent();


        // **********************************************************************
        // Name
        var name_textBox = new ValidationTextBox({
            name: 'name',
            label: 'Name',
            placeHolder: 'Enter a name',
            required: true,
            value: name
        }, 'filename_template_name');
        name_textBox.startup();


        // **********************************************************************
        // Target Entity Type
        // No update needed here just use a simple memory
        var target_entity_type_memory = new Memory({
            data: [
                {% for entity_type in entity_types %}
                    {
                        name: '{{ entity_type }}',
                        id: '{{ entity_type }}'
                    },
                {% endfor %}
            ]
        });

        {% if mode=='CREATE' %}
            target_entity_type = target_entity_type_memory.data[0].id;
        {% endif %}

        var target_entity_type_select = new FilteringSelect({
            name: 'target_entity_type',
            label: 'Target Entity Type',
            required: true,
            store: target_entity_type_memory,
            value: target_entity_type
        }, 'filename_template_target_entity_type');
        target_entity_type_select.startup();


        // **********************************************************************
        // Path
        var path_textBox = new TextBox({
            name: 'path',
            label: 'Path Template',
            placeHolder: 'Enter Jinja2 code here',
            required: true,
            value: path
        }, 'filename_template_path');
        path_textBox.startup();


        // **********************************************************************
        // Filename
        var filename_textBox = new TextBox({
            name: 'filename',
            label: 'Filename Template',
            placeHolder: 'Enter Jinja2 code here',
            required: true,
            value: filename
        }, 'filename_template_filename');
        filename_textBox.startup();


        // **********************************************************************
        // Ok Button
        var ok_button = new Button({
            label: 'OK',
            type: 'button',
            onClick: function () {
                submitForm({
                    dialog: dialog,
                    form: filename_template_form,
                    additional_data: additional_data,
                    url: url,
                    method: 'POST'
                });
            }
        }, 'filename_template_ok_button');
        ok_button.startup();


        // **********************************************************************
        // Cancel Button
        var cancel_button = new Button({
            label: 'Cancel',
            type: 'button',
            onClick: function () {
                dialog.destroyRecursive();
            }
        }, 'filename_template_cancel_button');
        cancel_button.startup();

    });
</script>

